package com.gitee.huanminabc.utils_tools.web_base.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//解决跨域问题
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//设置允许跨域的路径
//                .allowedOrigins("*")//允许跨域的域名，可以用*表示允许任何域名使用 ,2.4.0版本后使用allowedOriginPatterns
                .allowedOriginPatterns("*")//允许跨域的域名，可以用*表示允许任何域名使用
                .allowCredentials(true)//是否允许cookie
                .allowedHeaders("*")  // 设置允许的header属性
                .allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
                .maxAge(3600);//跨域允许时间
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**","/templates/**","/public/**")
                .addResourceLocations("classpath:/static/","classpath:/templates/","classpath:/public/");

    }

}
